function $(id){
    return document.getElementById(id);
}

window.onload = function (){
    cutByItem(1);
}

/**
 * 动态条件分页查询
 * @param pageNO 页码
 */
function cutByItem(pageNO){
    axios.get("/em/cutByItem",{
        params:{
            pageNO,
            name:$("searchName").value,
            startDate:$("startDate").value,
            endDate:$("endDate").value
        }
    }).then(resp =>{
        var info = resp.data;
        var str = "";
        info.records.forEach(n =>{
           str += `<tr><td>${n.name}</td><td>${n.birthday}</td>
                <td>${n.money}</td>
                <td><input type="button" value="删除" onclick="del(${n.id})">
                <input type="button" value="修改" onclick="findById(${n.id})"></td></tr>`;
        });
        $("data").innerHTML = str;

        var cutStr = "";
        for(let i=1;i<=info.pages;i++){
            cutStr += `<a style="margin-left: 10px" href="javascript:cutByItem(${i})">${i}</a>`;
        }
        $("cutDiv").innerHTML = cutStr;
    });
}

/**
 * 添加员工
 */
function addEm(){
    axios.get("/em/add",{
        params: {
            name:$("addName").value,
            birthday:$("addBirthday").value,
            money:$("addMoney").value
        }
    }).then(resp =>{
        var info = resp.data;
        if(info == "ok"){
            $("addDiv").style.visibility = "hidden";
            cutByItem(1);
        }
    })
}

function del(id){
    axios.get("/em/del",{
        params:{
            id
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            cutByItem(1);
        }
    })
}

function findById(id){
    axios.get("/em/findById",{
        params:{
            id
        }
    }).then(resp =>{
        var obj = resp.data;
        $("updateDiv").style.visibility = "visible";
        $("updateId").value = id;
        $("nameSpan").innerHTML = obj.name;
        $("birthdaySpan").innerHTML = obj.birthday;
        $("updateMoney").value = obj.money;
    })
}

function updateEm(){
    axios.get("/em/update",{
        params:{
            id:$("updateId").value,
            money: $("updateMoney").value
        }
    }).then(resp =>{
        if(resp.data == "ok"){
            $("updateDiv").style.visibility = "hidden";
            cutByItem(1);
        }
    })
}